package com.zhaopin.theswordreferstooffer;

/**
 * @author yin.peide
 * @date 2021-06-25 14:03
 * 面试题33: 把数组排成最小的数
 * 跟个冒泡似的 按照冒泡的逻辑来搞即可，最后返回整个数组就是排好序的了
 */
public class Offer33 {

    public static void main(String[] args) {

        int[] attr = {3, 32, 321};
        System.out.println(printMinNumber(attr));

    }

    public static String printMinNumber(int[] numbers) {
        if (numbers == null || numbers.length == 0) {
            return "";
        }
        for (int i = 0; i < numbers.length; i++) {
            for (int j = i + 1; j < numbers.length; j++) {
                int sum1 = Integer.valueOf(numbers[i] + "" + numbers[j]);
                int sum2 = Integer.valueOf(numbers[j] + "" + numbers[i]);
                if (sum1 > sum2) {
                    int temp1 = numbers[i];
                    int temp2 = numbers[j];
                    numbers[i] = temp2;
                    numbers[j] = temp1;
                }
            }
        }
        String str = "";
        for (int i = 0; i < numbers.length; i++) {
            str = str + numbers[i];
        }
        return str;

    }
}
